package com.tm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tm.ResultEntity;
import com.tm.pay.config.WechatConfig;
import com.tm.service.CommonService;
import com.tm.util.CommonUtilsEx;
import com.tm.util.HttpRemoteCall;
import com.tm.util.JSONUtilsEx;
import com.tm.util.WeixinConfigUtil;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * APP首页模块相关接口
 * @author GXL
 *
 * 2017年12月6日
 * TODO 处理APP首页各模块内容展示等相关业务
 */
@RestController
@RequestMapping(value = "/common")
public class CommonController {
	
	private Logger logger = Logger.getLogger(CommonController.class);
	
	@Autowired
	private CommonService cService;
	
    /**
     * 区域级联选择
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value="区域级联选择",notes="参境区域级联选择接口")
    @RequestMapping(value="/areaChoose", method=RequestMethod.POST)
    public ResultEntity areaChoose(HttpServletRequest request, HttpServletResponse response) {
    	ResultEntity re = null;
		System.out.println("-------------------------------app区域级联选择接口begin-----------------------------------------");
		try {
				
			Integer areaId = Integer.parseInt(request.getParameter("areaId"));
			
			re = cService.getAreaByParentAreaId(areaId);
			
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			re = new ResultEntity("-1", "系统异常！", response);
		}
		System.out.println("-------------------------------app区域级联选择接口end-----------------------------------------");
		
        return re;
	}
    
    /**
     * 根据区域名称或区域id获取区域信息
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value="获取区域信息",notes="参境获取区域信息接口")
    @RequestMapping(value="/areaInfo", method=RequestMethod.POST)
    public ResultEntity areaInfo(HttpServletRequest request, HttpServletResponse response) {
    	ResultEntity re = null;
		System.out.println("-------------------------------app获取区域信息接口begin-----------------------------------------");
		try {
				
			String name = request.getParameter("areaName");
			String id = request.getParameter("areaId");
			String lat = request.getParameter("lat");//纬度
			String lng = request.getParameter("lng");//经度
			String distance = request.getParameter("distance");//门店距离范围
			
			re = cService.getEcAreaByParam(name, id, lat, lng, distance);
			
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			re = new ResultEntity("-1", "系统异常！", response);
		}
		System.out.println("-------------------------------app获取区域信息接口end-----------------------------------------");
		
        return re;
	}
    
    
    /**
     * APP根据验证银行卡基本信息
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value="验证银行卡基本信息",notes="参境验证银行卡基本信息接口")
    @RequestMapping(value="/checkBankCard", method=RequestMethod.POST)
    public ResultEntity checkBankCard(HttpServletRequest request, HttpServletResponse response) {
    	ResultEntity re = null;
		String msg = "";
		System.out.println("-------------------------------app验证银行卡基本信息接口begin-----------------------------------------");
		try {
				
			String cardNo = request.getParameter("cardNo");
			
			String param = "_input_charset=utf-8&cardNo=" + cardNo + "&cardBinCheck=true";
			String jsonStr = CommonUtilsEx.sendPost("https://ccdcapi.alipay.com/validateAndCacheCardInfo.json", param);
			Map<String, Object> res = JSONUtilsEx.deserialize(jsonStr, Map.class);
			
			if(((Boolean) res.get("validated")).booleanValue()){
				
				String bank = String.valueOf(res.get("bank"));
				
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("logo", "https://apimg.alipay.com/combo.png?d=cashier&t=" + bank);
				m.put("bankno", bank);
				
				//根据银行编号获取银行LOGO
				re = new ResultEntity("0", null, m);
				
			}else{
				re = new ResultEntity("-1", "卡号输入有误！");
			}
			
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg, response);
		}
		System.out.println("-------------------------------app验证银行卡基本信息接口end-----------------------------------------");
		
        return re;
	}
    
    
    /**
     * APP搜索银行支行
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value="搜索银行支行",notes="参境搜索银行支行接口")
    @RequestMapping(value="/bankBranch", method=RequestMethod.POST)
    public ResultEntity bankBranch(HttpServletRequest request, HttpServletResponse response) {
    	ResultEntity re = null;
		String msg = "";
		System.out.println("-------------------------------app搜索银行支行接口begin-----------------------------------------");
		try {
				
				String city = request.getParameter("city");//城市名称
				String bankNo = request.getParameter("bankNo");//银行卡号
				String branch = request.getParameter("branch");//支行名称
				
				re = cService.getBankBranch(city, bankNo, branch);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg, response);
		}
		System.out.println("-------------------------------app搜索银行支行接口end-----------------------------------------");
		
        return re;
	}
    
    /**
	 * 获取资讯列表
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value="获取资讯列表",notes="获取资讯列表接口")
    @RequestMapping(value="/infoList", method=RequestMethod.POST)
    public ResultEntity collectList(HttpServletRequest request, HttpServletResponse response){
		ResultEntity re = null;
		
		String msg = "";
		System.out.println("-------------------------------获取资讯列表接口begin-----------------------------------------");
		try {
			
			Integer startIndex = Integer.parseInt(request.getParameter("startIndex"));//开始下标
			Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));//每页显示数量
			
			re = cService.getTtHeadlinesList(startIndex, pageSize);
				
		} catch (NumberFormatException cce) {
			System.out.println(cce.getMessage());
			re = new ResultEntity("-1", "参数类型有误！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			System.out.println("-------------------------------请求出错-----------------------------------------");
			System.out.println(e.getMessage());
			msg = e.getMessage();
			re = new ResultEntity("-1", msg);
		}
		System.out.println("-------------------------------获取资讯列表接口end-----------------------------------------");
		
        return re;
    }
    
}
